package com.xl.exp.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.LoggerFactory;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * shiro 自定义realm
 */
public class CustomRealm extends AuthorizingRealm {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(CustomRealm.class);

    /* 测试数据 */
    Map<String,String> map = new HashMap<>();

    {
        map.put("mark", "85ba00896f2b52db37560294050bcca0");

        super.setName("customRealm");
    }

    /**
     *  验证用户权限信息
     * @param principals 认证信息
     * @return 权限信息
     */
    @Override//doGetAuthenticationInfo
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        String username = (String)principals.getPrimaryPrincipal();
        logger.debug("#### AuthorizationInfo ---->" + username);

        Set<String> roles = getRolesByUserName(username);
        Set<String> permissions = getPermissionByUserName(username);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setRoles(roles);
        simpleAuthorizationInfo.setStringPermissions(permissions);

        return simpleAuthorizationInfo;
    }

    /**
     *
     * @param username 用户名
     * @return 模拟数据
     */
    private Set<String> getPermissionByUserName(String username) {
        Set<String> set = new HashSet<>();
        set.add("user:delete");
        set.add("user:select");
        set.add("system");
        return set;
    }

    /**
     *
     * @param username 用户名
     * @return 模拟数据
     */
    private Set<String> getRolesByUserName(String username) {

        Set<String> set = new HashSet<>();
        set.add("admin");
        set.add("user");
        set.add("system");
        return set;
    }

    /**
     *  用户认证
     * @param token 用户名和密码
     * @return 认证信息
     * @throws AuthenticationException 认证异常
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String  username = (String) token.getPrincipal();
        logger.debug("#### AuthorizationInfo ---->" + username);
        String password = getPasswordByUserName(username);

        if(password == null && password.equals(""))
            return null;

        AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                username, password,"customRealm"
        );
        return authenticationInfo;
    }

    /**
     * 模拟更具用户名和密码登录
     * @param username
     * @return
     */

    private String getPasswordByUserName(String username) {
        return map.get(username);
    }

    /**
     * 测试
     * @param args 默认参数
     */
    public static void main(String[] args) {
        Md5Hash md5Hash = new Md5Hash("111111","mark");
        System.out.println(md5Hash);
    }
}
